package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.CombinationBaseInfoDao;
import com.JZ.QM.common.dao.ProDetailDao;
import com.JZ.QM.common.entity.CombinationBaseInfoEntity;
import com.JZ.QM.common.entity.ProDetailEntity;
import com.JZ.QM.common.entity.ProYieldEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;

import com.JZ.QM.common.dao.CombinationYieldDao;
import com.JZ.QM.common.entity.CombinationYieldEntity;
import com.JZ.QM.service.CombinationYieldService;

import javax.annotation.Resource;

/**
 *
 *
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020-06-14 11:19:36
 */
@Service
public class CombinationYieldServiceImpl extends ServiceImpl<CombinationYieldDao, CombinationYieldEntity> implements CombinationYieldService {
    @Resource
    CombinationYieldDao combinationYieldDao;
    @Resource
    ProDetailDao proDetailDao;
    @Resource
    CombinationBaseInfoDao combinationBaseInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<CombinationYieldEntity> page = this.page(
                new Query<CombinationYieldEntity>().getPage(params),
                new QueryWrapper<CombinationYieldEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据组合id获取收益信息
     * @param comBaseId 组合的基本信息id
     * @return
     */
    @Override
    public HashMap<String, Object> getCombinationYields(Long comBaseId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //交易日期数组
        String[] tradeDates;
        //收益信息列表
        BigDecimal[] combinationIncomes;
        //组合的索引
        int combiantionIndex=0;
        //交易日期与收益列表
        HashMap<String,Object> proResult = new HashMap<>();
        //组合收益信息实体列表
        ArrayList<CombinationYieldEntity> combinationYieldEntities;
        //交易的天数
        int size=0;

        queryWrapper.select("com_yield_money","trade_date")
                .eq("com_base_id",comBaseId);
        queryWrapper.orderByAsc("trade_date");
        combinationYieldEntities= (ArrayList) combinationYieldDao.selectList(queryWrapper);
        size=combinationYieldEntities.size();
        tradeDates=new String[size];
        combinationIncomes=new BigDecimal[size];
        //筛选出收益信息并返回
        for (Iterator<CombinationYieldEntity> it = combinationYieldEntities.iterator(); it.hasNext(); ) {
            CombinationYieldEntity iter = it.next();
            combinationIncomes[combiantionIndex]=iter.getComYieldMoney();
            tradeDates[combiantionIndex]=iter.getTradeDate();
            combiantionIndex++;
        }
        proResult.put("newComIncome",combinationIncomes);
        proResult.put("newProTradeDate",tradeDates);
        return proResult;
    }

    /**
     * 甲酸组合的收益信息
     * @param updateDate
     * @return
     */
    @Override
    public boolean updateCombinationDailyIncomeRadio(String updateDate) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //组合基本信息列表
        ArrayList<CombinationBaseInfoEntity> combinationBaseInfoEntities=new ArrayList<>();
        //组合id
        Long comBaseId;
        //产品详细信息列表
        ArrayList<ProDetailEntity>  proDetailEntities=new ArrayList<>();
        //产品详细信息
        ProDetailEntity proDetailEntity=new ProDetailEntity();
        //组合收益率
        BigDecimal combinationYield = new BigDecimal(0);
        //产品所占比例
        BigDecimal shareRadio=new BigDecimal(0);
        //产品日常收益率
        BigDecimal shareIncomeDailtRadio=new BigDecimal(0);
        //组合收益基本信息
        CombinationYieldEntity combinationYieldEntity=new CombinationYieldEntity();
        //影响的函数
        int affectRow=0;

        queryWrapper.select("com_base_id")
                .eq("is_used",2);
        //获取正在上架的组合信息
        combinationBaseInfoEntities= (ArrayList<CombinationBaseInfoEntity>) combinationBaseInfoDao.selectList(queryWrapper);
        for (Iterator<CombinationBaseInfoEntity> it = combinationBaseInfoEntities.iterator(); it.hasNext(); ) {
            comBaseId = it.next().getComBaseId();
            //获取组合的基本详情
            proDetailEntities=proDetailDao.getComProDetail(comBaseId,updateDate);
            for (Iterator<ProDetailEntity> iterator = proDetailEntities.iterator(); iterator.hasNext(); ) {
                proDetailEntity = iterator.next();
                shareRadio=proDetailEntity.getComShareRadio();
                shareIncomeDailtRadio = proDetailEntity.getProIncomeDailyRadio()==null? new BigDecimal(0):proDetailEntity.getProIncomeDailyRadio();
                System.out.println("原组合"+shareRadio.toString()+"%%%%%%%%%%%%%%%%%%%%%%%%%"+shareIncomeDailtRadio.toString());
                combinationYield=combinationYield.add(shareRadio.multiply(shareIncomeDailtRadio));
            }
            combinationYieldEntity.setComBaseId(comBaseId);
            combinationYieldEntity.setComYieldMoney(combinationYield);
            combinationYieldEntity.setTradeDate(updateDate);
            affectRow=combinationYieldDao.insert(combinationYieldEntity);
            if( affectRow ==0){
                return false;
            }
            combinationYield=new BigDecimal(0);
        }
        return true;
    }

    /**
     * 获取原组合的平均日收益率
     * @param comBaseId 原组合id
     * @return
     */
    @Override
    public BigDecimal getComAverageDailyRadio(Long comBaseId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //平均日收益率
        BigDecimal comAverage=new BigDecimal(0);

        comAverage=combinationYieldDao.getAverageYield(comBaseId);
        return comAverage;
    }

}